const Subscription = require('egg').Subscription;

class TaskCrawler extends Subscription {
  // 通过 schedule 属性来设置定时任务的执行间隔等配置
  static get schedule() {
    return {
      cron: '0 2 5 * * ?',
      type: 'worker',
      env: ['pachong'],
      // immediate: true
    };
  }

  async subscribe() {
    const {ctx} = this;

    let param = {
      url: 'http://www.biqu5200.net/75_75584/',
      author_info: {
        author_id: '62a6b3cce0db57a40eeaf204',
        author_name: '剑来-笔趣阁',
        author_avatar: 'https://taosha01-1253585015.cos.ap-shanghai.myqcloud.com/typora/jianlai.png'
      },
      topic_info: {
        tab: 'yutang',
      },
      selector_info: {
        box: '#list > dl > dd',
        title: 'a',
        content: 'a',
      },
    };

    let topics = await ctx.service.commonSvc.commonFetch(param);
    topics = topics.slice(0, 50);

    let result = await this.ctx.service.topicSvc.importTopics(topics);
    this.logger.info(`${param.author_info.author_name} ready ${topics.length} success ${result}`);
  }
}

module.exports = TaskCrawler;
